﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using OpenWaves.Web.Controls;

namespace OpenWaves.Web.Showcase
{
    public partial class Tree : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DataBind();
        }

        protected IEnumerable<MyTreeNode> RootNodes
        {
            get
            {
                return new[]
                    {
                        new MyTreeNode("Root 1",
                            new MyTreeNode("Child 1-1",
                                new MyTreeNode("Child 1-1-1"),
                                new MyTreeNode("Child 1-1-2")),
                            new MyTreeNode("Child 1-2")), 
                        new MyTreeNode("Root2")
                    };
            }
        }
    }

    public class TreeView : TreeView<MyTreeNode>
    {
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(TreeNodeContainer<MyTreeNode>))]
        public ITemplate NodeTemplate { get; set; }

        protected override ITemplate ResolveNodeTemplate(MyTreeNode node, int level)
        {
            return this.NodeTemplate;
        }
    }

    public class MyTreeNode : ITreeNode<MyTreeNode>
    {
        private readonly MyTreeNode[] children;

        public MyTreeNode(string title, params MyTreeNode[] children)
        {
            this.children = children;
            this.Title = title;
        }

        public IEnumerable<MyTreeNode> Children
        {
            get { return this.children; }
        }

        public string Title { get; set; }
    }
}